package com.ukao.steward.ui.menuLeft.myEarnings;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.MyEarningBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.DecimalUtil;
import com.ukao.steward.util.T;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 我的收入
 */
public class MyEarningsFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    @BindView(R.id.left_bar_btn)
    ImageView leftBarBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.today_earnings_layout)
    RelativeLayout todayEarningsLayout;
    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.balance_txt)
    TextView balanceTxt;

    @BindView(R.id.chage_layout)
    LinearLayout chageLayout;
    @BindView(R.id.withdraw_layout)
    LinearLayout withdrawLayout;
    @BindView(R.id.mouth_earnings_layout)
    RelativeLayout mouthEarningsLayout;
    public MyEarningBean mMyEarningBean;
    @BindView(R.id.toDayIncome)
    TextView toDayIncome;
    @BindView(R.id.monthIncome)
    TextView monthIncome;

    private String mParam1;
    private String mParam2;
    private boolean isloadFinish = false;
    public String withdrawBalance="0.00";

    public static MyEarningsFragment newInstance(String param1, String param2) {
        MyEarningsFragment fragment = new MyEarningsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my_earnings, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        initToolbarNav(leftBarBtn);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        setStatusBarTintColor(R.color.colorPrimary);
        balanceIncome();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.balance_txt, R.id.chage_layout, R.id.withdraw_layout, R.id.today_earnings_layout, R.id.mouth_earnings_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.balance_txt:
                start(BalanceListFragment.newInstance("", "","","",-1));
                break;
            case R.id.chage_layout:
               // start(AccountChageFragment.newInstance("", ""));
                break;
            case R.id.withdraw_layout:
                if (mMyEarningBean==null)return;
                startForResult(WithdrawFragment.newInstance(withdrawBalance, mMyEarningBean.getMinExtractMoney()),Constant.REQUEST_CODE);
                break;
            case R.id.today_earnings_layout:

                start(TodayEarningsFragment.newInstance("1", 0));
                break;
            case R.id.mouth_earnings_layout:

                start(TodayEarningsFragment.newInstance("2", 0));
                break;
        }
    }

    public void balanceIncome() {
        if (isloadFinish) return;
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().balanceIncome(Constant.createParameter(mHashMap)),
                new ApiCallback<MyEarningBean>() {

                    @Override
                    public void onSuccess(MyEarningBean model) {

                        if (model.getHttpCode() == 200) {
                            isloadFinish = true;
                            mMyEarningBean = model.getData();
                            withdrawBalance=CheckUtils.isEmptyNumber(mMyEarningBean.getBalance());
                            toDayIncome.setText(CheckUtils.isEmptyNumber(mMyEarningBean.getToDayIncome()));
                            monthIncome.setText(CheckUtils.isEmptyNumber(mMyEarningBean.getMonthIncome()));

                            balance.setText(withdrawBalance);
                        }else{
                            T.show(model.getMsg());
                        }

                    }

                    @Override
                    public void onFinish() {

                        dismissProgressDialog();


                    }
                });
    }



    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constant.REQUEST_CODE:

                    if (!CheckUtils.isNull(mMyEarningBean)){
                       String   withdrawamount=data.getString(Constant.ARG_PARAM1);
                        String strbalance = CheckUtils.isEmptyNumber(mMyEarningBean.getBalance());
                         withdrawBalance = DecimalUtil.subtract(strbalance, withdrawamount);
                        balance.setText(withdrawBalance);
                    }

                    break;
            }
        }
    }





}
